package com.ruoyi.web.controller.yf;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.yf.service.IYfTagService;
import com.ruoyi.yf.tool.GenerateDeptName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yf.domain.YfDept;
import com.ruoyi.yf.service.IYfDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 医院科室Controller
 *
 * @author XiaoYi...
 * @date 2020-03-11
 * 2b9b5484-8506-4d47-ba81-2c9c8f6dd926
 */
@Controller
@RequestMapping("/yf/dept")
public class YfDeptController extends BaseController {
    private String prefix = "yf/dept";

    private static final Logger log = LoggerFactory.getLogger(YfDeptController.class);

    @Autowired
    private IYfDeptService yfDeptService;
    @Autowired
    private IYfTagService yfTagService;
    /**
     *
     * <p/>
     *
     * <p/>
     *
     * @param	file
     * @param	updateSupport
     * @return com.ruoyi.common.core.domain.AjaxResult
     *
     * @throws
     * @author  XiaoYi...
     * @date    2021/1/28 4:42 下午
     * 7f7fa943-1559-4147-a0d1-8181a18075ff
     */
    @Log(title = "科室管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:dept:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<YfDept> util = new ExcelUtil<YfDept>(YfDept.class);
        List<YfDept> deptList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = yfDeptService.importDept(deptList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:dept:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<YfDept> util = new ExcelUtil<YfDept>(YfDept.class);
        return util.importTemplateExcel("科室数据");
    }
    @RequiresPermissions("yf:dept:view")
    @GetMapping()
    public String dept(ModelMap mmap) {
//        List<SysDept> sysDept = deptService.selectHospital(ShiroUtils.getSysUser().getDeptId());
//        mmap.put("deptList", sysDept);
        return prefix + "/dept";
    }

    @RequiresPermissions("yf:dept:view")
    @GetMapping("/{hostId}")
    public String dept(@PathVariable("hostId") Long hostId, ModelMap mmap) {
        System.out.println("列表点击:" + hostId);

        //
//        SysDept sysDept = deptService.selectDeptById(hostId);
//        mmap.put("dept", sysDept);
        return prefix + "/dept";
    }

    /**
     * 查询医院科室列表
     * 0b821eb0-0b0d-4fc0-b28b-8c14d7d4753b
     */
    @RequiresPermissions("yf:dept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YfDept yfDept) {
        //System.out.println( " 开始时间： " +  new SimpleDateFormat("HH:mm:ss:SSS").format( new Date()) );
        startPage();
        List<YfDept> list = yfDeptService.selectYfDeptList(yfDept);
        //System.out.println( " 结束时间： " +  new SimpleDateFormat("HH:mm:ss:SSS").format( new Date()) );
        return getDataTable(list);
    }

    /**
     * XiaoYi...
     * 通过医院查询 科室
     *
     * @Date : 2020-03-18 14:38
     * b7e9632a-374f-4eb6-9826-1e5f6aa01b40
     */
    @RequiresPermissions("yf:dept:list")
    @PostMapping("/adds/list")
    @ResponseBody
    public TableDataInfo getDeptlist(@RequestParam("stagingId") Long stagingId) {
        List<YfDept> list = yfDeptService.selectHD(stagingId);
        return getDataTable(list);
    }

    /**
     * 通过医院查询 科室
     *b1fae498-e478-4e7d-8360-03e4fe6d1488
     */
    @PostMapping("/getYfDeptListByHospitalId")
    @ResponseBody
    public TableDataInfo getYfDeptListByHospitalId(@RequestParam("hosid") Long hosid) {
        List<YfDept> list = yfDeptService.getYfDeptListByHospitalId(hosid);
        return getDataTable(list);
    }





    /**
     * 导出医院科室列表
     * 96379fe3-3e4e-4f99-9ce5-a6ff38440f50
     */
    @RequiresPermissions("yf:dept:export")
    @Log(title = "医院科室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YfDept yfDept) {
        List<YfDept> list = yfDeptService.selectYfDeptList(yfDept);
        ExcelUtil<YfDept> util = new ExcelUtil<YfDept>(YfDept.class);
        return util.exportExcel(list, "dept");
    }

    /**
     * 新增医院科室
     * 02f6e256-6de0-4692-9af6-7a26f5b0a569
     */
    @GetMapping("/add/{dept}")
    public String add(@PathVariable("dept") String dept, ModelMap mmap) {
        System.out.println(dept);
        mmap.put("hostId", dept);
        return prefix + "/add";
    }

    /**
     * 新增保存医院科室
     * 918fcc2c-4a4c-4ecf-a388-5321752e3fac
     */
    @RequiresPermissions("yf:dept:add")
    @Log(title = "医院科室", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YfDept yfDept) {
        //创建科室号、添加到对象
        yfDept.setDeptId(GenerateDeptName.deptName_15());
        return toAjax(yfDeptService.insertYfDept(yfDept));
    }

    /**
     * 修改医院科室
     * 3513f860-71ca-4632-8925-469f35dc79aa
     */
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap) {
        YfDept yfDept = yfDeptService.selectYfDeptById(deptId);
        mmap.put("yfDept", yfDept);
        return prefix + "/edit";
    }

    @RequiresPermissions("yf:dept:list")
    @Log(title = "医院科室", businessType = BusinessType.UPDATE)
    @PostMapping("/byId")
    public AjaxResult getDept(Long deptId) {
        YfDept data = yfDeptService.selectYfDeptById(deptId);
        return toAjaxFull(data);
    }

    /**
     * 修改保存医院科室
     * 68c54574-f9d2-4564-9125-877927038573
     */
    @RequiresPermissions("yf:dept:edit")
    @Log(title = "医院科室", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YfDept yfDept) {
        if(yfDept.getStatus().equals("1")){
            int sum = yfDeptService.selectDeptTagSum(yfDept.getDeptId() + "" );
            //如果科室存在条码，不允许停用
            if (sum > 0){
                return AjaxResult.error("科室下存在" + sum + "条医废记录不允许停用");
            }
        }
        return toAjax(yfDeptService.updateYfDept(yfDept));
    }

    /**
     * 彻底删除医院科室
     * 6a98aaf5-8c6a-48e1-9d5e-0d000dc78f6b
     */
    @RequiresPermissions("yf:dept:remove")
    @Log(title = "医院科室", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int sum = yfDeptService.selectDeptTagSum(ids);
        if (sum > 0)
            return AjaxResult.error("科室下存在" + sum + "条医废记录不允许删除");
        return toAjax(yfDeptService.deleteYfDeptByIds(ids));
    }


    /**
     * 假删除科室 标记删除
     *
     * @param ids
     * @return
     * 478bda3e-b128-47e3-a079-69c546465e01
     */
    @RequiresPermissions("yf:dept:uRemove")
    @Log(title = "科室", businessType = BusinessType.FAKE_DELETE)
    @PostMapping("/Uremove")
    @ResponseBody
    public AjaxResult uRremove(String ids) {
        int sum = yfDeptService.selectDeptTagSum(ids);
        //如果科室存在条码，不允许删除，未存在则逻辑删除
        if (sum > 0){
            return AjaxResult.error("科室下存在" + sum + "条医废记录不允许删除");
        }
        // return toAjax(yfDeptService.deleteYfDeptByIds(ids));     // 彻底删除
        return toAjax(yfDeptService.updateRremoveAndRecoveryTagByIds(ids, UserConstants.DELETE_STATUS));    // 逻辑删除
    }


    /**
     * 恢复假删除科室 标记删除
     *
     * @param ids
     * b4d8c072-a483-4d32-b838-2c2307b4337a
     * @return
     */
    @RequiresPermissions("yf:dept:uRecovery")
    @Log(title = "科室", businessType = BusinessType.Update_Recovery)
    @PostMapping("/URecovery")
    @ResponseBody
    public AjaxResult URecovery(String ids) {
        return toAjax(yfDeptService.updateRremoveAndRecoveryTagByIds(ids, UserConstants.RECOVER_STATUS));
    }


}
